# # 1.2 打印设备名 和 IPv4 地址
# import socket
# # 设备名
# host_name = socket.gethostname()
# print(host_name)
# # IPv4 地址
# ipv4 = socket.gethostbyname(host_name)
# print(ipv4)

# 1.3 获取远程设备的 IP 地址
# import socket
# remote_ipv4 = socket.gethostbyname("www.baidu.com")
# print("Baidu: "+remote_ipv4)

# 1.4 ipv4 格式转换
# import socket
# import struct
# ip_group = ["127.0.0.1", "192.168.0.1"]
#
# def covert_ipv4_address():
#     for ip in ip_group:
#         packed_addr = socket.inet_aton(ip)
#         """
#         inet_aton 源码
#             def inet_aton(string):
#                 return b''
#         """
#         unpacked_addr = socket.inet_ntoa(packed_addr)
#         print("IP Address: {} => Packed: {}, Unpacked: {}".format(ip, packed_addr, unpacked_addr))
#
#
# if __name__ == "__main__":
#     covert_ipv4_address()

# 1.5 通过指定端口和协议找到服务名
# import socket
# port_group = [80, 443, 22, 21, 25, 53]
#
# def find_service_name():
#     for port in port_group:
#         protocol = 'tcp'
#         print(socket.getservbyport(port, protocol))
#
#
# find_service_name()

# # 1.6 主机字节序和网络字节序之间的相互转换
# import socket
#
# def convert_integer():
#     data = 1234
#     # 32 bit
#     print("Origin: {}, Long host byte: {}, Network byte order: {}".format(
#         data,
#         socket.ntohl(data),
#         socket.ntohl(data)
#     ))
#     # 16 bit
#     print("Origin: {}, Short host byte: {}, Network byte order: {}".format(
#         data,
#         socket.ntohs(data),
#         socket.htons(data)
#     ))
#
#
# convert_integer()

# # 设置并获取默认套接字的超时时间
# import socket
#
# def testTimeout():
#     # 创建套接字对象
#     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#     print("Default socket {}".format(s.gettimeout()))
#     s.settimeout(100)
#     print("Current socket {}".format(s.gettimeout()))
#
# testTimeout()

# 编写一个简单的回显客户端/服务端应用

# import socket
# import sys
# import argparse
#
# host = 'localhost'
# data_payload = 2048
# backlog = 5
#
# def echo_server(port):
#     # Create a TCP socket
#     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#     # enable reuse address port
#     sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#     # Bind the socket to the port
#     server_addr = (host, port)
#     print("Starting up echo server on %s:%s" % server_addr)
#     sock.bind(server_addr)
#     # Listen to clients
#     sock.listen(backlog)
#     while True:
#         print("Waiting to receive message from client")
#         client, address = sock.accept()
#         data = client.recv(data_payload)
#         if data:
#             print("Data: {}".format(data))
#             print(data)
#             client.send(data)
#             print("sent {} bytes back to {}".format(data, address))
#         client.close()
#
#
# if __name__ == '__main__':
#     parser = argparse.ArgumentParser(description='Socket Server Example')
#     parser.add_argument('--port', action='store', dest='port', type=int, required=True)
#     given_args = parser.parse_args()
#     port = given_args.port
#     echo_server(port)


#!/usr/bin/python3
# 文件名：server.py

# 导入 socket、sys 模块
import socket
import sys

# 创建 socket 对象
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 获取本地主机名
host = socket.gethostname()

port = 9999

# 绑定端口号
serversocket.bind((host, port))

# 设置最大连接数，超过后排队
serversocket.listen(5)

while True:
    # 建立客户端连接
     clientsocket,addr = serversocket.accept()
     print("连接地址: %s" % str(addr))
     msg='欢迎访问菜鸟教程！'+ "\r\n"
     clientsocket.send(msg.encode('utf-8'))
     clientsocket.close()